package com.hejielife.twophasetermination2;

/**
 * 2019年8月2日 下午4:55:28
 * 
 * @author hejielife
 * @despribe CountupThread
 */

public class CountupThread extends Thread {
	private long counter = 0;

	public void shutdownRequest() {
		interrupt();
	}

	public void run() {
		try {
			while (!isInterrupted()) {
				doWork();
			}
		} catch (InterruptedException e) {
		} finally {
			doShutdown();
		}
	}

	// throws InterruptedException 可以中断
	private void doWork() throws InterruptedException {
		counter++;
		System.out.println("doWork: counter = " + counter);
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
		}
	}

	private void doShutdown() {
		System.out.println("doShutdown: counter = " + counter);
	}
}
